from django.db import models
from django.contrib.auth.models import User, Group

# Create your models here.

class UserProfile(models.Model):
    """用户扩展信息表"""
    user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name='用户', related_name='profile', db_comment='关联用户ID')
    
    # 上级归属关系
    supervisor = models.ForeignKey(
        User, 
        on_delete=models.SET_NULL, 
        null=True, 
        blank=True, 
        verbose_name='上级主管', 
        related_name='subordinates',
        db_comment='上级主管ID'
    )
    
    # 学生信息字段
    student_id = models.CharField(max_length=50, blank=True, unique=True, null=True, verbose_name='学号', db_comment='学生学号')
    major = models.CharField(max_length=100, blank=True, verbose_name='专业', db_comment='所属专业')
    class_name = models.CharField(max_length=100, blank=True, verbose_name='班级', db_comment='所属班级')
    gender = models.CharField(
        max_length=10, 
        blank=True, 
        choices=[('男', '男'), ('女', '女')], 
        verbose_name='性别',
        db_comment='性别（男/女）'
    )
    
    # 英语四六级成绩字段
    cet4_score = models.IntegerField(
        null=True,
        blank=True,
        verbose_name='英语四级成绩',
        db_comment='英语四级考试成绩'
    )
    cet4_status = models.CharField(
        max_length=20,
        blank=True,
        default='未提交',
        choices=[
            ('未提交', '未提交'),
            ('审核中', '审核中'),
            ('已通过', '已通过'),
            ('未通过', '未通过')
        ],
        verbose_name='四级审核状态',
        db_comment='英语四级成绩审核状态'
    )
    cet4_exemption_pic = models.URLField(
        max_length=500,
        blank=True,
        verbose_name='四级免修证明图片',
        db_comment='英语四级免修证明截图（带姓名）'
    )
    cet4_score_pic = models.URLField(
        max_length=500,
        blank=True,
        verbose_name='四级成绩证明图片',
        db_comment='英语四级成绩证明截图（带姓名和分数）'
    )
    cet4_reject_reason = models.TextField(
        blank=True,
        verbose_name='四级拒绝原因',
        db_comment='英语四级成绩审核拒绝原因'
    )
    cet6_score = models.IntegerField(
        null=True,
        blank=True,
        verbose_name='英语六级成绩',
        db_comment='英语六级考试成绩'
    )
    cet6_status = models.CharField(
        max_length=20,
        blank=True,
        default='未提交',
        choices=[
            ('未提交', '未提交'),
            ('审核中', '审核中'),
            ('已通过', '已通过'),
            ('未通过', '未通过')
        ],
        verbose_name='六级审核状态',
        db_comment='英语六级成绩审核状态'
    )
    cet6_exemption_pic = models.URLField(
        max_length=500,
        blank=True,
        verbose_name='六级免修证明图片',
        db_comment='英语六级免修证明截图（带姓名）'
    )
    cet6_score_pic = models.URLField(
        max_length=500,
        blank=True,
        verbose_name='六级成绩证明图片',
        db_comment='英语六级成绩证明截图（带姓名和分数）'
    )
    cet6_reject_reason = models.TextField(
        blank=True,
        verbose_name='六级拒绝原因',
        db_comment='英语六级成绩审核拒绝原因'
    )
    
    # 扩展字段
    phone = models.CharField(max_length=20, blank=True, verbose_name='手机号', db_comment='联系电话')
    department = models.CharField(max_length=100, blank=True, verbose_name='部门', db_comment='所属部门')
    position = models.CharField(max_length=100, blank=True, verbose_name='职位', db_comment='职位名称')
    employee_id = models.CharField(max_length=50, blank=True, unique=True, null=True, verbose_name='工号', db_comment='员工工号')
    hire_date = models.DateField(null=True, blank=True, verbose_name='入职日期', db_comment='入职日期')
    remarks = models.TextField(blank=True, verbose_name='备注', db_comment='备注信息')
    
    # 元数据
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', db_comment='记录创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间', db_comment='记录更新时间')
    
    class Meta:
        verbose_name = '用户扩展信息'
        verbose_name_plural = '用户扩展信息'
        ordering = ['-created_at']
        db_table_comment = '用户扩展信息表，存储学生和员工的详细信息'
    
    def __str__(self):
        return f"{self.user.username} (ID: {self.pk or 'N/A'})"
    
    def get_all_subordinates(self):
        """递归获取所有下级用户（包括下级的下级）"""
        subordinates = []
        direct_subordinates = User.objects.filter(profile__supervisor=self.user)
        
        for subordinate in direct_subordinates:
            subordinates.append(subordinate)
            if hasattr(subordinate, 'profile'):
                subordinates.extend(subordinate.profile.get_all_subordinates())
        
        return subordinates

